import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        tableData: [],
    },
    // 获取数据
    mutations: {
        setList(state, objData) {
            // console.log(objData);
            state.tableData = objData
        },
        // 增加
        addItem(state, item) {
            state.tableData.push(item)
        },
        // 改
        chengeItem(state, changeData) {
            state.tableData.forEach((item) => {
                if (item.id == changeData.id) {
                    item = changeData
                }
            })
        },
        // 删
        delItem(state, id) {
            // state.tableData.splice(id, 1)
            const i = state.tableData.findIndex(e => e.id === id)
            i > -1 && state.tableData.splice(i, 1)
        }
    },
    // 发送请求
    actions: {
        async gitList(axt) {
            const { data: res } = await axios({
                url: '/list.json'
            })

            axt.commit('setList', res)
        }
    },
})